/* File makes Table 9 */

/* 	Input files: 	temp_follows, mktret10day, crsp_returns, permno_symbol_match_final, 
	Output files: T9_Follow_leave.tex */

**********************************************
clear all
macro drop _all
scalar drive		= "E:"
scalar maindir		= "`=drive'\Replicate"
scalar tabledir		= "`=drive'\Replicate\TablesRep"
cd `=maindir'
**********************************************


use temp_follows, clear  

keep bull userid symboli date morebull_11_30 morebear_11_30 morebear_31_50 morebull_31_50 

merge m:1 date using mktret10day, keep(master match)  nogen 
save temp_leave, replace

use date permno xret_10d_f2 using crsp_returns, clear 
merge m:1 permno using permno_symbol_match_final, keep(match) nogen 
	drop comnam symbol_title tsymbol symbol_ticker

merge 1:m date symbolid using temp_leave, keep(match using) nogen
	drop if mi(xret_10d_f2)


local retvar "vwretd_10d_f2"
gen mktretnegative=cond(`retvar'<0,1,0) 
gen mktretp50=cond(`retvar'<.0064754,1,0) 
	drop sprtrn_10d_f2 vwretd_10d_f2 morebear_31_50 morebull_31_50 permno
	
	

local lhsvar "morebull_11_30"

eststo bull0: reghdfe `lhsvar' bull , absorb(i.userid i.symbolid#i.date) vce(cl userid) 
	qui sum `lhsvar' if e(sample)==1
	qui estadd scalar avg = r(mean)
	scalar define avg= r(mean)


gen mvar=mktretp50
eststo bull1: reghdfe `lhsvar' bull 1.bull#1.mvar , absorb(i.userid i.symbolid#i.date) vce(cl userid)  
	qui sum `lhsvar' if e(sample)==1
	qui estadd scalar avg = r(mean)
	scalar define avg= r(mean)


cap drop mvar
gen mvar=mktretnegative
eststo bull2: reghdfe `lhsvar' bull 1.bull#1.mvar, absorb(i.userid i.symbolid#i.date) vce(cl userid) 
	qui sum `lhsvar' if e(sample)==1
	qui estadd scalar avg = r(mean)
	scalar define avg= r(mean)


gen negret=cond(xret_10d_f2<0,1,0)
gen p50ret=cond(xret_10d_f2<-0.338,1,0) 
drop xret_10d_f2


cap drop ivar
gen ivar=p50ret
eststo bull3: reghdfe `lhsvar' bull 1.bull#1.ivar, absorb(i.userid i.symbolid#i.date) vce(cl userid) 
	qui sum `lhsvar' if e(sample)==1
	qui estadd scalar avg = r(mean)
	scalar define avg= r(mean)


cap drop ivar
gen ivar=negret
eststo bull4: reghdfe `lhsvar' bull 1.bull#1.ivar, absorb(i.userid i.symbolid#i.date) vce(cl userid) 
	qui sum `lhsvar' if e(sample)==1
	qui estadd scalar avg = r(mean)
	scalar define avg= r(mean)


cap drop ivar
gen ivar=p50ret
cap drop mvar
gen mvar=mktretp50
eststo bull5: reghdfe `lhsvar' bull 1.bull#1.ivar 1.bull#1.mvar, absorb(i.userid i.symbolid#i.date) vce(cl userid) 
	qui sum `lhsvar' if e(sample)==1
	qui estadd scalar avg = r(mean)
	scalar define avg= r(mean)

	
cap drop ivar
gen ivar=negret
cap drop mvar
gen mvar=mktretnegative
eststo bull6: reghdfe `lhsvar' bull 1.bull#1.ivar 1.bull#1.mvar, absorb(i.userid i.symbolid#i.date) vce(cl userid) 
	qui sum `lhsvar' if e(sample)==1
	qui estadd scalar avg = r(mean)
	scalar define avg= r(mean)




gen bear=cond(bull==0,1,0)


local lhsvar "morebear_11_30"

eststo bear0: reghdfe `lhsvar' bear , absorb(i.userid i.symbolid#i.date) vce(cl userid) 
	qui sum `lhsvar' if e(sample)==1
	qui estadd scalar avg = r(mean)
	scalar define avg= r(mean)


cap drop mvar
gen mvar=mktretp50
eststo bear1: reghdfe `lhsvar' bear 1.bear#1.mvar, absorb(i.userid i.symbolid#i.date) vce(cl userid) 
	qui sum `lhsvar' if e(sample)==1
	qui estadd scalar avg = r(mean)
	scalar define avg= r(mean)


cap drop mvar
gen mvar=mktretnegative
eststo bear2: reghdfe `lhsvar' bear 1.bear#1.mvar, absorb(i.userid i.symbolid#i.date) vce(cl userid) 
	qui sum `lhsvar' if e(sample)==1
	qui estadd scalar avg = r(mean)
	scalar define avg= r(mean)


cap drop ivar
gen ivar=p50ret
eststo bear3: reghdfe `lhsvar' bear 1.bear#1.ivar, absorb(i.userid i.symbolid#i.date) vce(cl userid)  
	qui sum `lhsvar' if e(sample)==1
	qui estadd scalar avg = r(mean)
	scalar define avg= r(mean)


cap drop ivar
gen ivar=negret
eststo bear4: reghdfe `lhsvar' bear 1.bear#1.ivar, absorb(i.userid i.symbolid#i.date) vce(cl userid) 
	qui sum `lhsvar' if e(sample)==1
	qui estadd scalar avg = r(mean)
	scalar define avg= r(mean)


cap drop ivar
gen ivar=p50ret
cap drop mvar
gen mvar=mktretp50
eststo bear5: reghdfe `lhsvar' bear 1.bear#1.ivar 1.bear#1.mvar, absorb(i.userid i.symbolid#i.date) vce(cl userid) 
	qui sum `lhsvar' if e(sample)==1
	qui estadd scalar avg = r(mean)
	scalar define avg= r(mean)


cap drop ivar
gen ivar=negret
cap drop mvar
gen mvar=mktretnegative
eststo bear6: reghdfe `lhsvar' bear 1.bear#1.ivar 1.bear#1.mvar, absorb(i.userid i.symbolid#i.date) vce(cl userid) 
	qui sum `lhsvar' if e(sample)==1
	qui estadd scalar avg = r(mean)
	scalar define avg= r(mean)
	cap estadd local userfE "Y"

*****************************************************************************************************


cap estadd local dsymbolFe "Y" : *
cap estadd local usfE "Y" : *

#delimit ; 
local stats_opts " stats(N N_clust r2 avg usfE dsymbolFe, fmt(%18.0fc %18.0fc %12.2fc %12.2fc %12.0fc %12.0fc) labels("\# observations" "\# clusters (users)" "$\textit{R}^2$" "Mean of dependent var.($\%$)" "User FE" "Day x stock FE") ) " ;

local opts  "b(%16.2fc) se(%16.2fc) brackets booktabs  star(* 0.10 ** 0.05 *** 0.01) nonotes lines 
mtitles(
"Baseline"
"\shortstack{$\times$ MktRet \\ < p50}"
"\shortstack{$\times$ MktRet \\ < 0}"
	"\shortstack{$\times$ Ret\textsubscript{s} \\ < p50}"
	"\shortstack{$\times$ Ret\textsubscript{s} \\ < 0}"
	"\shortstack{$\times$ both \\ < p50}"
	"\shortstack{$\times$ both \\ < 0}"

"Baseline"
"\shortstack{$\times$ MktRet \\ < p50}"
"\shortstack{$\times$ MktRet \\ < 0}"
	"\shortstack{$\times$ Ret\textsubscript{s} \\ < p50}"
	"\shortstack{$\times$ Ret\textsubscript{s} \\ < 0}"
	"\shortstack{$\times$ both \\ < p50}"
	"\shortstack{$\times$ both \\ < 0}"
)

mgroups("Dep. var.: $\mathbbm{1}$ x100 if net new Bull follows\textsubscript{i,s,(t+11 $\rightarrow$ t+30)} >0" 
"$\mathbbm{1}$ x100 if net new Bear follows\textsubscript{i,s,(t+11 $\rightarrow$ t+30)} >0" 
, pattern(1 0 0 0 0 0 0 1 0 0 0 ) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span}))  " ;   

local filename "T9_Follow_leave" ;


cd "`=tabledir'" ;
esttab bull0 bull1 bull2 bull3 bull4 bull5 bull6 bear0 bear1 bear2 bear3 bear4 bear5 bear6   using `filename'.tex, `opts' `stats_opts' 
drop(_cons) coef(
bull 	"$\mathbbm{1}$ Declare Bull\textsubscript{i,s,t}" 
bear 	"$\mathbbm{1}$ Declare Bear\textsubscript{i,s,t}"
1.bull#1.mvar 	"$\mathbbm{1}$ Bull\textsubscript{i,s,t} x $\mathbbm{1}$ MktRet\textsubscript{(t+2 $\rightarrow$ 10)}< p50 or 0" 
1.bear#1.mvar 	"$\mathbbm{1}$ Bear\textsubscript{i,s,t} x $\mathbbm{1}$ MktRet\textsubscript{(t+2 $\rightarrow$ 10)}< p50 or 0" 

1.bull#1.ivar 	"$\mathbbm{1}$ Bull\textsubscript{i,s,t} x $\mathbbm{1}$ Ret\textsubscript{s,(t+2 $\rightarrow$ 10)}< p50 or 0" 
1.bear#1.ivar 	"$\mathbbm{1}$ Bear\textsubscript{i,s,t} x $\mathbbm{1}$ Ret\textsubscript{s,(t+2 $\rightarrow$ 10)}< p50 or 0" 

) replace ;
#delimit cr 
cd "`=maindir'" 


cap n erase temp_leave.dta




